{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from graphviz import Digraph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'测试图片.gv.pdf'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = Digraph('测试图片')\n",
    "g.node(name='a',color='red')\n",
    "g.node(name='b',color='blue')\n",
    "g.edge('a','b',color='green')\n",
    "g.view()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "// the test\n",
      "digraph MyPicture {\n",
      "\ta [label=Ming color=green]\n",
      "\tb [label=Hong color=yellow]\n",
      "\tc [label=Dong]\n",
      "\ta -> b [label=\"ab\n",
      "a-b\" color=red]\n",
      "\tc -> b\n",
      "\ta -> c\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "# 实例化一个Digraph对象(有向图)，name:生成的图片的图片名，format:生成的图片格式\n",
    "dot = Digraph(name=\"MyPicture\", comment=\"the test\", format=\"png\")\n",
    "\n",
    "# 生成图片节点，name：这个节点对象的名称，label:节点名,color：画节点的线的颜色\n",
    "dot.node(name='a', label='Ming', color='green')\n",
    "dot.node(name='b', label='Hong', color='yellow')\n",
    "dot.node(name='c', label='Dong')\n",
    "\n",
    "# 在节点之间画线，label：线上显示的文本,color:线的颜色\n",
    "dot.edge('a', 'b', label=\"ab\\na-b\", color='red')\n",
    "# 一次性画多条线，c到b的线，a到c的线\n",
    "dot.edges(['cb', 'ac'])\n",
    "\n",
    "# 打印生成的源代码\n",
    "print(dot.source)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'mypicture.png'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 画图，filename:图片的名称，若无filename，则使用Digraph对象的name，默认会有gv后缀\n",
    "# directory:图片保存的路径，默认是在当前路径下保存\n",
    "dot.view(filename=\"mypicture\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'MyPicture.png'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 跟view一样的用法(render跟view选择一个即可)，一般用render生成图片，不使用view=True,view=True用在调试的时候\n",
    "dot.render(filename='MyPicture',view=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'MyPicture.png'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dot.view()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
